package tamtoucan.friendly;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.EntityInteractEvent;

public class EntityInteractHandler {

	@ForgeSubscribe
	public boolean onEntityInteract(EntityInteractEvent event) {

		EntityPlayer player = event.entityPlayer;
		ItemStack itemstack = player.inventory.getCurrentItem();
		
		if ((itemstack != null) && (event.target instanceof EntityCreeper))
		{
			EntityCreeper c = (EntityCreeper) event.target;
			if (itemstack.itemID == Item.gunpowder.itemID)
			{
				if(!player.capabilities.isCreativeMode)
					--itemstack.stackSize;

				if(itemstack.stackSize <= 0)
					player.inventory.setInventorySlotContents(player.inventory.currentItem, (ItemStack)null);

				if(!player.worldObj.isRemote)
				{
					EntityTameableCreeper newCreeper = new EntityTameableCreeper(c.worldObj);
					newCreeper.setLocationAndAngles(c.posX, c.posY, c.posZ, c.rotationYaw, c.rotationPitch);
					c.worldObj.spawnEntityInWorld(newCreeper);
					c.setDead();
					return true;
				}
			}
		}
		return false;
	}
}
